package com.sugon.controller.basics;

import com.sugon.annotation.AutoLog;
import com.sugon.entity.KnowBaseEntity;
import com.sugon.service.KnowBaseService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 知识库Controller
 *
 * @author sugon
 * @date 2019-07-10 15:18:30
 */
@Controller
@RequestMapping("knowbase")
public class KnowBaseController {
    @Autowired
    private KnowBaseService knowBaseService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<KnowBaseEntity> knowBaseList = knowBaseService.queryList(query);
        int total = knowBaseService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(knowBaseList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        KnowBaseEntity knowBase = knowBaseService.queryObject(id);

        return R.ok().put("knowBase", knowBase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("knowbase:save")
    @ResponseBody
    public R save(@RequestBody KnowBaseEntity knowBase) {
        knowBase.setCreateUser(getUserId());
        knowBase.setCreateDate(new Date());
        knowBase.setIsDel(1);
        knowBaseService.save(knowBase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("knowbase:update")
    @ResponseBody
    public R update(@RequestBody KnowBaseEntity knowBase) {
        knowBase.setUpdateUser(getUserId());
        knowBase.setUpdateDate(new Date());
        knowBaseService.update(knowBase);

        return R.ok();
    }

    /**
     * 修改删除状态
     */
    @RequestMapping("/changeDelCode")
    @RequiresPermissions("knowbase:changeDelCode")
    @ResponseBody
    public R changeDelCode(@RequestBody String[] ids) {
      /*  KnowBaseEntity knowBaseEntity = knowBaseService.queryObject(id);
        knowBaseEntity.setIsDel(0);*/
        knowBaseService.updateBatch(ids);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("knowbase:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        knowBaseService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<KnowBaseEntity> list = knowBaseService.queryList(params);

        return R.ok().put("list", list);
    }
}
